//
//  MessageBackController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/22.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "MessageBackController.h"
#import "MLAlertController.h"
#import "MLTextview.h"

#define kMaxTextViewCount 200

@interface MessageBackController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>


@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UILabel *titleLab;

@property (nonatomic, strong) MLTextview  *textview;

@property (nonatomic, strong) UIButton *sendMsgBack; //提交反馈

@property (nonatomic, strong) UILabel  *titleCountLab; //您还可以输入的字数

@end

@implementation MessageBackController


static NSString *ID = @"settingCell";


- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
}

-(void)setupUI{
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = kNaiBaiHuiColor;
    
    
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.font  = FONT(17);
    self.titleLab.text = @"欢迎联系我们";
    
    [self.view addSubview:self.tableview];
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.titleLab];
    [self.bgView addSubview:self.textview];
    
    [self.bgView addSubview:self.sendMsgBack];
    [self.bgView addSubview:self.titleCountLab];
    
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(300);
        
    }];
    
    [self.textview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.bgView.mas_top).offset(10+64);
        make.left.mas_equalTo(self.bgView.mas_left).offset(10);
        make.right.mas_equalTo(self.bgView.mas_right).offset(-10);
        make.bottom.mas_equalTo(self.bgView.mas_bottom).offset(-45);;
        
    }];
    
    //提交反馈
    [self.sendMsgBack mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.textview.mas_bottom).offset(4);
        make.bottom.mas_equalTo(self.bgView.mas_bottom).offset(-4);
        make.right.mas_equalTo(-10);
        make.width.mas_equalTo(90);
    }];
    
    
    //倒数个数
    [self.titleCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.textview.mas_bottom);
        make.bottom.mas_equalTo(self.bgView.mas_bottom).offset(0);
        make.right.mas_equalTo(self.sendMsgBack.mas_left).offset(-10);
        make.width.mas_equalTo(220);
    }];
    
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.bgView.mas_bottom).offset(20);
    }];
    
    
    
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(10);
    }];
    
    
}

#pragma mark  - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
    if (indexPath.row==0) {
        
        cell.imageView.image = [UIImage imageNamed:@"dianhua"];
        cell.textLabel.text = @"官服电话：400-636-6113";
    }else{
        cell.imageView.image = [UIImage imageNamed:@"qq"];
        cell.textLabel.text = @"用户交流群：QQ 464768569";
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        
        [MLAlertController alertWithVC:self Title:@"温馨提示" message:@"是否拨打电话：400-636-6113" cancelTitle:@"取消" cancelBlock:nil sureTitle:@"确定" sureBlock:^{
            loggerWarn(@"拨打电话");
        }];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    [self.view endEditing:YES];
}

#pragma mark  - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView{

    
    if ( textView.text.length <=kMaxTextViewCount) {
        
        self.titleCountLab.text = [NSString stringWithFormat:@"您还可以输入的字数为：%ld",kMaxTextViewCount-textView.text.length];
        
    }else{
    
        NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithString:self.titleCountLab.text];
 
        NSRange range = NSMakeRange(11, self.titleCountLab.text.length-11);
        [attM addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:range];
        
        self.titleCountLab.attributedText = attM;
    }
}


#pragma mark  - Actions
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];
}

- (void)clickSendMsgBack:(UIButton *)btn{

    
    if (self.textview.text.length == 0) {
        
        [SVPAlertManager alertInfoStatus:@"请输入反馈信息"];
        
        return;
    }else if(self.textview.text.length >kMaxTextViewCount){
    
        [SVPAlertManager alertErrorStatus:@"个数不能超过200字"];
        return;
    }
    [self.view endEditing:YES];
    [SVPAlertManager alertSuccessStatus:@"谢谢您的意见反馈"];
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark  - Lazy
-(UITableView *)tableview{
    
    if (_tableview == nil) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.dataSource = self;
        _tableview.delegate = self;
        _tableview.tableFooterView = [UIView new];
        [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    }
    return _tableview;
}

-(MLTextview *)textview{
    
    if (_textview == nil) {
        _textview = [[MLTextview alloc] init];
        _textview.backgroundColor = kNaiBaiHuiColor;
        _textview.delegate = self;
    }
    return _textview;
}

-(UIView *)bgView{
    
    if (_bgView == nil) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = WHITE_COLOR;
    }
    return _bgView;
}

-(UIButton *)sendMsgBack{
    
    if (_sendMsgBack == nil) {
        _sendMsgBack = [[UIButton alloc] init];
        [_sendMsgBack setTitle:@"提交反馈" forState:UIControlStateNormal];
        [_sendMsgBack setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        [_sendMsgBack addTarget:self action:@selector(clickSendMsgBack:) forControlEvents:UIControlEventTouchUpInside];
        _sendMsgBack.layer.cornerRadius = 3;
        _sendMsgBack.layer.masksToBounds = YES;
        
        _sendMsgBack.backgroundColor = kThemeColor;
    }
    return _sendMsgBack;
}

-(UILabel *)titleCountLab{
    
    if (_titleCountLab == nil) {
        _titleCountLab = [[UILabel alloc] init];
        _titleCountLab.text = @"您还可以输入的字数为：200";
        _titleCountLab.font = FONT(16);
        _titleCountLab.textAlignment  =NSTextAlignmentLeft;
    }
    return _titleCountLab;
}

@end
